﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using aztall.Module.BusinessObjects;

namespace aztall.Module.Controllers
{
	// For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
	public partial class PivotActions_Controller : ViewController
	{
		string[] ShowPivotDialogTargetViewes = new string[] { "Promo_PromoCosts_ListView", "Promo_PromoVols_ListView", "DW_ListView" };
		ListView thisView = null;
		Promo _Promo;
		public PivotActions_Controller()
		{
			InitializeComponent();
			// Target required Views (via the TargetXXX properties) and create their Actions.
			this.ShowPivotDialog.TargetViewId = ShowPivotDialogTargetViewes.Aggregate((current, next) => current + ";" + next);
			this.ShowPivotDialog.TargetViewType = ViewType.ListView;
		}
		protected override void OnActivated()
		{
			base.OnActivated();
			// Perform various tasks depending on the target View.
			thisView = View as ListView;
			#region set master object
			if (thisView != null && thisView.CollectionSource is PropertyCollectionSource)
			{
				PropertyCollectionSource collectionSource = (PropertyCollectionSource)thisView.CollectionSource;
				collectionSource.MasterObjectChanged += CollectionSource_MasterObjectChanged;
				if (collectionSource.MasterObject != null)
					UpdateMasterObject(collectionSource.MasterObject);
			}
			#endregion
		}
		private void CollectionSource_MasterObjectChanged(object sender, EventArgs e)
		{
			UpdateMasterObject(((PropertyCollectionSource)sender).MasterObject);
		}
		void UpdateMasterObject(object masterObject)
		{
			_Promo = masterObject as Promo;
		}
		protected override void OnViewControlsCreated()
		{
			base.OnViewControlsCreated();
			// Access and customize the target View control.
		}
		protected override void OnDeactivated()
		{
			// Unsubscribe from previously subscribed events and release other references and resources.
			base.OnDeactivated();
		}

		private void ShowPivotDialog_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e)
		{
			View v = null;
			switch (View.Id)
			{
				case "Promo_PromoCosts_ListView":
					v = base.Application.CreateDetailView(this.ObjectSpace, "PromoCost_AdvanceInput", false, _Promo);
					break;
				case "Promo_PromoVols_ListView":
					v = base.Application.CreateDetailView(this.ObjectSpace, "PromoVolume_AdvanceInput", false, _Promo);
					break;
				case "DW_ListView":
					if (thisView.CollectionSource is CollectionSource)
					{
						v = base.Application.CreateDetailView(this.ObjectSpace, "DW_AdvanceInput", false, thisView.CollectionSource);
					}
					break;
			}
			e.DialogController.SaveOnAccept = false;
			e.View = v;
		}
	}
}
